package com.sgb.kjwl.view.ui.activity.self.logistics;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityInquiryBinding;
import com.sgb.kjwl.model.entity.transport.CargoPublishEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.SingleItemEntity;
import com.sgb.kjwl.utils.CargoConstant;
import com.sgb.kjwl.utils.Constants;
import com.sgb.kjwl.utils.DefaultUtil;
import com.sgb.kjwl.utils.NumberUtil;
import com.sgb.kjwl.utils.PickerHelper;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.constant.RequestCode;
import com.sgb.kjwl.view.constant.RouterKey;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.WebEvent;
import com.sgb.kjwl.view.ui.activity.WebActivity;
import com.sgb.kjwl.view.ui.adapter.CargoMoreAdapter;
import com.sgb.kjwl.view.ui.adapter.self.logistics.CargoPublishPersonAdapter;
import com.swgk.core.dialog.RoundDialog;
import com.sgb.kjwl.viewmodel.self.logistics.InquiryModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.ArrayUtil;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.TimeUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 杨畔
 * @date: 4/6/21 2:43 PM
 * @Description: 询价
 */
public class InquiryActivity extends AppActivity {
    InquiryModel viewModel;
    ActivityInquiryBinding binding;
    //选择器
    private OptionsPickerView<GoodsInfoTypeEntity3> mLoadModePicker;
    private TimePickerView mDatePicker;
    //发货地/收货地列表
    private int mLocationIndex;//选中地址下标
    private List<CargoPublishEntity.LocationEntity> mLocationList;
    private CargoPublishPersonAdapter mLocationAdapter;
    //付款约定信息列表
    private List<SingleItemEntity> mPayList;
    private CargoMoreAdapter mPayAdapter;
    //违约责任信息列表
    private List<SingleItemEntity> mContractList;
    private CargoMoreAdapter mContractAdapter;
    //数据实体类
    private CargoPublishEntity mEntity;
    //车辆no
    private String mNo;
    //0:车辆询价 1:承运询价
    private int mType;

    /**
     * 跳转车辆询价
     *
     * @param no 车辆no
     */
    public static void startCarInquiry(Context context, String no, String phone) {
        Intent intent = new Intent(context, InquiryActivity.class);
        intent.putExtra(RouterKey.TYPE, 0);
        intent.putExtra(RouterKey.NO, no);
        intent.putExtra(RouterKey.STRING, phone);
        context.startActivity(intent);
    }

    /**
     * 跳转承运询价
     *
     * @param no 车辆no
     */
    public static void startCarriageInquiry(Context context, String no, String phone) {
        Intent intent = new Intent(context, InquiryActivity.class);
        intent.putExtra(RouterKey.TYPE, 1);
        intent.putExtra(RouterKey.NO, no);
        intent.putExtra(RouterKey.STRING, phone);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        viewModel = new ViewModelProvider(this).get(InquiryModel.class);
    }

    @Override
    protected void init() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_inquiry);
        binding.setLifecycleOwner(this);
        //初始化页面
        initPicker();
        initRecycler();
        initEvent();
        initData();
    }

    private void initPicker() {
        //报价截止时间选择器
        mDatePicker = PickerHelper.with(this).getCommonTimeBuild("选择报价截止时间", (date, v) -> {
            long time = DateUtils.getCustomizeDayNight(0, date.getTime()).getTime();
            if (mEntity.getEndTime() != 0 && time > mEntity.getEndTime()) {
                MToast.showToast(this, "报价截止时间不能大于装货结束时间");
                return;
            }
            //设置数据
            mEntity.setBiddingEndTime(time);
            binding.tvReleaseCargoQuotation.setText(DateUtils.stampToDay5(String.valueOf(time)));
        })
                .setType(new boolean[]{true, true, true, false, false, false})
                .build();
    }

    private void initRecycler() {
        //发货人/收货人列表
        mLocationList = new ArrayList<>();
        mLocationAdapter = new CargoPublishPersonAdapter(mLocationList);
        binding.rvReleaseCargoPerson.setNestedScrollingEnabled(false);
        binding.rvReleaseCargoPerson.setLayoutManager(new LinearLayoutManager(this));
        mLocationAdapter.bindToRecyclerView(binding.rvReleaseCargoPerson);
        //付款约定列表
        mPayList = new ArrayList<>();
        mPayAdapter = new CargoMoreAdapter(mPayList);
        binding.rvReleaseCargoPayAgreement.setNestedScrollingEnabled(false);
        binding.rvReleaseCargoPayAgreement.setLayoutManager(new LinearLayoutManager(this));
        mPayAdapter.bindToRecyclerView(binding.rvReleaseCargoPayAgreement);
        //违约责任列表
        mContractList = new ArrayList<>();
        mContractAdapter = new CargoMoreAdapter(mContractList);
        binding.rvReleaseCargoContract.setNestedScrollingEnabled(false);
        binding.rvReleaseCargoContract.setLayoutManager(new LinearLayoutManager(this));
        mContractAdapter.bindToRecyclerView(binding.rvReleaseCargoContract);
    }

    private void initEvent() {
        binding.setOnClick(this);
        binding.includeTitle.ivBack.setOnClickListener(this);
        mLocationAdapter.setOnItemClickListener((adapter, view, position) -> {
            mLocationIndex = position;
            CargoPublishPersonActivity.start(this,
                    mLocationList.get(position), RequestCode.CODE_SEVEN);
        });
        //接口回调
        viewModel.ldPublish.observe(this, this::syncData);
        viewModel.ldHandleType.observe(this, this::initLoadPicker);
        viewModel.ldDistance.observe(this, entity -> {
            //设置总运程数据
            mEntity.setDistance(entity);
            if (entity == null || entity.getTotalDistance() == null) {
                binding.llReleaseCargoDistance.setVisibility(View.GONE);
            } else {
                binding.llReleaseCargoDistance.setVisibility(View.VISIBLE);
                binding.tvReleaseCargoDistance.setText(String.format("约%skm", NumberUtil.trim0(entity.getTotalDistance() / 1000)));
            }
        });
        //跳转回调
        viewModel.ldJump.observe(this, integer -> {
            if (integer == 0) {
                //跳转费用信息
                CargoPublishCostActivity.start(this, mEntity, RequestCode.CODE_THREE);
            }
        });
        //定位回调
        viewModel.ldLocation.observe(this, aBoolean -> {
            mLocationAdapter.notifyDataSetChanged();
            //重算运程
            calculateLineDistance();
        });
    }

    private void initData() {
        String phone = getIntent().getStringExtra(RouterKey.STRING);
        viewModel.setPhone(phone);
        //
        mNo = getIntent().getStringExtra(RouterKey.NO);
        mType = getIntent().getIntExtra(RouterKey.TYPE, 0);
        mEntity = new CargoPublishEntity();
        initCommon();
        //获取页面数据
        viewModel.getHandleType();
    }

    /**
     * 初始化装卸方式弹窗
     */
    private void initLoadPicker(List<GoodsInfoTypeEntity3> list) {
        //装卸方式选择器
        mLoadModePicker = PickerHelper.with(this).getCommonPicker("选择装卸方式", list, (options1, options2, options3, v) -> {
            GoodsInfoTypeEntity3 entity = list.get(options1);
            mEntity.setLoadModeNo(entity.getNo());
            mEntity.setLoadMode(entity.getName());
            binding.tvReleaseCargoLoadMode.setText(entity.getName());
            //设置列表数据
            viewModel.getLocationListByLoadMode(mLocationList, entity.getNo());
            mLocationAdapter.notifyDataSetChanged();
            //重算运程
            calculateLineDistance();
        });
    }

    /**
     * 编辑货源时同步货源数据使用
     */
    private void syncData(CargoPublishEntity entity) {
        if (entity == null) {
            boolean isNormal = getIntent().getBooleanExtra(RouterKey.BOOLEAN, true);
            mEntity = new CargoPublishEntity();
            mEntity.setPattern(isNormal ? 0 : 1);
            initCommon();
            return;
        }
        mEntity = entity;
        initCommon();
        //同步数据
        syncDate(mEntity);
        syncGoods(mEntity);
        syncCost(mEntity);
        syncCar(mEntity);
        syncMoreData(mEntity);
        //设置竞价结束时间
        if (mEntity.getPattern() == 1 && mEntity.getBiddingEndTime() != 0) {
            binding.tvReleaseCargoQuotation.setText(DateUtils.stampToDay5(String.valueOf(mEntity.getBiddingEndTime())));
        }
    }

    /**
     * 设置通用数据
     */
    private void initCommon() {
        //设置标题
        binding.includeTitle.titleView.setText("询价");
        //设置默认数据
        if (TextUtils.isEmpty(mEntity.getCompanyNo())) {
            //设置用户信息
            mEntity.setCompanyNo(UserOperating.getInstance().getTeamOrCompanyNameNo());
            mEntity.setCompany(UserOperating.getInstance().getTeamOrCompanyName());
            mEntity.setContact(UserOperating.getInstance().getUserRealName());
            mEntity.setContactTel(UserOperating.getInstance().getPhone());
        }
        //设置装卸方式
        initDefaultLoadMode();
    }

    /**
     * 初始化默认装卸方式
     */
    private void initDefaultLoadMode() {
        //只有初始发布填写定位数据，历史货源/编辑货源数据不填写
        if (!TextUtils.isEmpty(mEntity.getLoadModeNo())) {
            //编辑货源/历史货源
            binding.tvReleaseCargoLoadMode.setText(mEntity.getLoadMode());
            viewModel.getLocationListByLoadMode(mLocationList, mEntity.getLoadModeNo());
            mLocationAdapter.notifyDataSetChanged();
            //重算运程
            calculateLineDistance();
        } else {
            mEntity.setLoadModeNo("11");
            mEntity.setLoadMode("一装一卸");
            binding.tvReleaseCargoLoadMode.setText(mEntity.getLoadMode());
            viewModel.getLocationListByLoadMode(mLocationList, mEntity.getLoadModeNo());
            viewModel.getCurrentLocation(this);
        }
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_release_cargo_history:
                //选择历史竞价货源
                CargoPublishHistoryActivity.start(this, 0, RequestCode.CODE_ZERO);
                break;
            case R.id.ll_release_cargo_load_mode:
                //选择装卸方式
                mLoadModePicker.show();
                break;
            case R.id.ll_release_cargo_load_date:
                //选择装货时间
                CargoPublishDateActivity.start(this, mEntity, RequestCode.CODE_ONE);
                break;
            case R.id.ll_release_cargo_cargo_info:
                //选择货物信息
                CargoPublishCargoInfoActivity.start(this, mEntity, RequestCode.CODE_TWO);
                break;
            case R.id.ll_release_cargo_price:
                //选择费用要求
                CargoPublishCostActivity.start(this, mEntity, RequestCode.CODE_THREE);
                break;
            case R.id.ll_release_cargo_car_info:
                if (TextUtils.isEmpty(mEntity.getGoodsModeNo())) {
                    MToast.showToast(this, "请先选择货物类型");
                } else {
                    //选择车辆信息
                    CargoPublishCarInfoActivity.start(this, mEntity, RequestCode.CODE_FOUR);
                }
                break;
            case R.id.ll_release_cargo_quotation:
                //选择报价截止时间
                mDatePicker.show();
                break;
            case R.id.ll_release_cargo_more:
                //选择更多信息
                CargoPublishMoreActivity.start(this, mEntity, RequestCode.CODE_FIVE);
                break;
            case R.id.tv_release_cargo_submit:
                //提交询价
                clearAllFocus();
                mEntity.setLocationList(mLocationList);
                viewModel.releaseCargo(this, binding.tvReleaseCargoSubmit, mType, mNo, mEntity);
                break;
            case R.id.tv_release_cargo_protocol:
                EventBus.getDefault().postSticky(WebEvent.make(EventConstant.EVENT_WEBVIEW, Constants.TRANSPORT_URL));
                WebActivity.start(this);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) return;
        //历史货源回调
        if (requestCode == RequestCode.CODE_ZERO && data != null) {
            String no = data.getStringExtra(RouterKey.NO);
            viewModel.getDetail(this, no, true);
            return;
        }
        //发货人/收货人回调
        if (requestCode == RequestCode.CODE_SEVEN) {
            syncLocation(data);
            //计算运程
            calculateLineDistance();
            return;
        }
        //货源子页面数据
        if (data == null) return;
        CargoPublishEntity entity = data.getParcelableExtra(RouterKey.DATA);
        if (entity == null) return;
        switch (requestCode) {
            case RequestCode.CODE_ONE:
                syncDate(entity);
                break;
            case RequestCode.CODE_TWO:
                syncGoods(entity);
                break;
            case RequestCode.CODE_THREE:
                syncCost(entity);
                break;
            case RequestCode.CODE_FOUR:
                syncCar(entity);
                break;
            case RequestCode.CODE_FIVE:
                syncMoreData(entity);
                break;
        }
    }

    /**
     * 同步装货时间
     */
    private void syncDate(CargoPublishEntity entity) {
        mEntity = entity;
        if (mEntity.getBeginTime() == 0) {
            binding.tvReleaseCargoLoadDate.setText("");
            return;
        }else {
            if (mEntity.getStartPointTime()==25?DateUtils.getCustomizeDayNight(0,mEntity.getBeginTime()).getTime()<DateUtils.getCustomizeDayNight(0, TimeUtil.currentTimeMillis()).getTime():mEntity.getBeginTime()<TimeUtil.currentTimeMillis()){
                binding.tvReleaseCargoLoadDate.setText("");
                return;
            }
        }
        //设置装货时间
        binding.tvReleaseCargoLoadDate.setText(CargoConstant.stampFormatListLoadTime(
                mEntity.getBeginTime(), mEntity.getStartPointTime(),
                mEntity.getEndTime(), mEntity.getEndPointTime()));
      /*  binding.tvReleaseCargoLoadDate.setText(String.format("%s—%s",
                DateUtils.stampToDay5(String.valueOf(mEntity.getBeginTime())),
                DateUtils.stampToDay5(String.valueOf(mEntity.getEndTime()))));*/
    }

    /**
     * 设置货源信息
     */
    private void syncGoods(CargoPublishEntity entity) {
        //判断货物类型是否改变，改变了清空车辆信息
        if (entity.getCarTypeList() == null || entity.getCarLenList() == null) {
            syncCar(entity);
        }
        mEntity = entity;
//        if (TextUtils.isEmpty(mEntity.getGoodsModeName())) {
//            binding.tvReleaseCargoCargoInfo.setText("");
//            return;
//        }
        StringBuilder builder = new StringBuilder();
        String name = entity.getGoodsModeName();
        if (!TextUtils.isEmpty(name)) {
            if (name.contains("/")) {
                name = name.substring(name.indexOf("/") + 1);
            }
            builder.append(name);
        }
        if (!TextUtils.isEmpty(mEntity.getPackageModeName())) {
            if (!TextUtils.isEmpty(name)) {
                builder.append("/");
            }
            builder.append(mEntity.getPackageModeName());
        }
        if (mEntity.getGoodsWeight() != 0) {
            builder.append("/").append(NumberUtil.trim0(mEntity.getGoodsWeight())).append("吨");
        }
        if (mEntity.getGoodsVolume() != 0) {
            builder.append("/").append(NumberUtil.trim0(mEntity.getGoodsVolume())).append("方");
        }
        binding.tvReleaseCargoCargoInfo.setText(builder.toString());
        //计算总运费
        adjustTotalFreight();
    }

    /**
     * 同步费用要求
     */
    private void syncCost(CargoPublishEntity entity) {
        mEntity = entity;
        //底部展示
        binding.tvReleaseCargoMsgPrice.setText(String.format("%s元", NumberUtil.trim0(mEntity.getMsgPrice())));
        //计算总运费
        adjustTotalFreight();
    }

    /**
     * 同步车辆信息
     */
    private void syncCar(CargoPublishEntity entity) {
        mEntity = entity;
        if (mEntity.getCarTypeList() == null || mEntity.getCarLenList() == null) {
            binding.tvReleaseCargoCarInfo.setText("");
            return;
        }
        StringBuilder builder = new StringBuilder();
        builder.append("/").append(entity.getUseCarType() == 1 ? "整车" : "零担");
        if (mEntity.getCarTypeList() != null && mEntity.getCarLenList() != null) {
            for (GoodsInfoTypeEntity3 type : mEntity.getCarTypeList()) {
                builder.append("/").append(type.getName());
            }
            builder.append(" ");
            for (GoodsInfoTypeEntity3 type : mEntity.getCarLenList()) {
                builder.append("/").append(type.getName());
            }
        }
        if (mEntity.getHeightHurdle() != 0) {
            builder.append(" 栏高").append(NumberUtil.trim0(mEntity.getHeightHurdle())).append("米");
        }
        if (mEntity.getIsDump() != null) {
            builder.append(" ").append(mEntity.getIsDump() == 1 ? "自卸" : "非自卸");
        }
        if (!TextUtils.isEmpty(builder.toString())) {
            binding.tvReleaseCargoCarInfo.setText(builder.substring(1));
        }
    }

    /**
     * 同步自定义字段信息
     */
    private void syncMoreData(CargoPublishEntity entity) {
        mEntity = entity;
        //违约责任
        syncContract(entity);
        //付款约定
        syncPayAgreement(entity);
        //整体判断内容
        if (ArrayUtil.isNullOrEmpty(mPayList) && ArrayUtil.isNullOrEmpty(mContractList)) {
            binding.tvReleaseCargoMore.setHint(R.string.release_cargo_more_hint);
            binding.lineReleaseCargoContractBottom.setVisibility(View.GONE);
        } else {
            binding.tvReleaseCargoMore.setHint(R.string.empty);
            binding.lineReleaseCargoContractBottom.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 同步付款约定
     */
    private void syncPayAgreement(CargoPublishEntity entity) {
        mEntity = entity;
        mPayList.clear();
        if (entity.getArrivalTime() != null && entity.getArrivalTime() != 0) {
            mPayList.add(new SingleItemEntity("到货时间", String.format("开始运输后%s天内", NumberUtil.trim0(mEntity.getArrivalTime()))));
        }
        if (mEntity.getPerformanceBond() != null && mEntity.getPerformanceBond() != 0) {
            mPayList.add(new SingleItemEntity("履约保证金", String.format("%s元", NumberUtil.trim0(mEntity.getPerformanceBond()))));
        }
        //todo 装车要求
        if (mEntity.getRequire() != 0) {
            mPayList.add(new SingleItemEntity("装车要求", CargoConstant.getLoadRequire(mEntity.getRequire())));
        }
        if (!TextUtils.isEmpty(entity.getPayType())) {
            mPayList.add(new SingleItemEntity("付款方式", DefaultUtil.getString(entity.getPayType())));
        }
        if (!TextUtils.isEmpty(entity.getReceiptAccountRequire())) {
            mPayList.add(new SingleItemEntity("收款账户要求", DefaultUtil.getString(entity.getReceiptAccountRequire())));
        }
        if (!TextUtils.isEmpty(entity.getOtherPaymentRequire())) {
            mPayList.add(new SingleItemEntity("付款要求", DefaultUtil.getString(entity.getOtherPaymentRequire())));
        }
        //发票要求信息匹配检测
        if (mEntity.isInvoiceChanges()) {
            mEntity.setInvoiceNo("");
            mEntity.setInvoice("");
            mEntity.setTariff(null);
            showFieldAlterDialog();
        } else if (!TextUtils.isEmpty(mEntity.getInvoice()) && mEntity.getTariff() != null && !TextUtils.isEmpty(mEntity.getInvoiceNo())) {
            if (mEntity.getInvoice().equals("不开发票")) {
                mPayList.add(new SingleItemEntity("发票要求", entity.getInvoice()));
            } else {
                String invoiceStr = NumberUtil.trim0(mEntity.getTariff()) + "%" + mEntity.getInvoice();
                mPayList.add(new SingleItemEntity("发票要求", invoiceStr));
            }
        }
        if (!TextUtils.isEmpty(mEntity.getInvoicingCompanyName())) {
            mPayList.add(new SingleItemEntity("开票信息", String.format("%s/%s",
                    mEntity.getInvoicingCompanyName(),
                    mEntity.getInvoicingTaxNumber())));
        }
        mPayAdapter.notifyDataSetChanged();
        //判断付款信息是否为空
        if (ArrayUtil.isNullOrEmpty(mPayList)) {
            binding.rvReleaseCargoPayAgreement.setVisibility(View.GONE);
        } else {
            binding.rvReleaseCargoPayAgreement.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 因 2.0.4 发票要求字段调整，提示用户重新选择
     */
    private void showFieldAlterDialog() {
        new RoundDialog.Builder(this)
                .title("提示")
                .content("平台发票要求已调整，使用历史货源询价需重新选择该信息")
                .confirm("我知道了")
                .singleBtn(true)
                .confirmCallback(v -> {
                    if (mEntity != null) {
                        mEntity.setInvoiceChanges(false);
                    }
                })
                .contentGravity(Gravity.CENTER)
                .cancelable(false)
                .show();
    }

    /**
     * 同步违约责任
     */
    private void syncContract(CargoPublishEntity entity) {
        mEntity = entity;
        mContractList.clear();
        if (!TextUtils.isEmpty(entity.getLostPromise())) {
            mContractList.add(new SingleItemEntity("货物损坏或丢失赔偿约定", DefaultUtil.getString(entity.getLostPromise())));
        }
        if (!TextUtils.isEmpty(entity.getOverduePromise())) {
            mContractList.add(new SingleItemEntity("逾期装货或送货责任约定", DefaultUtil.getString(entity.getOverduePromise())));
        }
        if (!TextUtils.isEmpty(entity.getOtherPromise())) {
            mContractList.add(new SingleItemEntity("其他约定", DefaultUtil.getString(entity.getOtherPromise())));
        }
        //自定义字段
        if (mEntity.getCustomList() != null && !mEntity.getCustomList().isEmpty()) {
            mContractList.addAll(mEntity.getCustomList());
        }
        mContractAdapter.notifyDataSetChanged();
        //判断数据是否为空
        if (ArrayUtil.isNullOrEmpty(mContractList)) {
            binding.rvReleaseCargoContract.setVisibility(View.GONE);
        } else {
            binding.rvReleaseCargoContract.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 同步收货/发货
     */
    private void syncLocation(Intent data) {
        if (data == null) return;
        CargoPublishEntity.LocationEntity locationEntity = data.getParcelableExtra(RouterKey.DATA);
        if (locationEntity == null) return;
        mLocationList.set(mLocationIndex, locationEntity);
        mLocationAdapter.notifyDataSetChanged();
        //重设缓存数据
        viewModel.setCacheLocation(locationEntity.getCacheIndex(), locationEntity);
    }

    /**
     * 计算运程
     */
    private void calculateLineDistance() {
        viewModel.calculateLineDistance(mLocationList, mEntity.getLoadModeNo());
    }

    /**
     * 调整运费(更改了货物信息和运费信息之后计算
     */
    private void adjustTotalFreight() {
        //设置普通货源运费展示
        if (mEntity.getFreightType() == 1 && mEntity.getFreight() == 0) {
            binding.tvReleaseCargoPrice.setText(String.format("信息费 ¥%s",
                    NumberUtil.trim0(mEntity.getMsgPrice())));
            binding.tvReleaseCargoFreight.setText(R.string.default_release_cargo_price);
            return;
        }
        if (mEntity.getFreight() == 0) {
            mEntity.setTotalFreight(0);
            binding.tvReleaseCargoFreight.setText(R.string.default_release_cargo_price);
            return;
        }
        //根据运费类型计算运费
        double total;
        switch (mEntity.getFreightMode()) {
            case CargoPublishEntity.FreightMode.MODE_ONCE:
                total = mEntity.getFreight();
                mEntity.setTotalFreight(total);
                binding.tvReleaseCargoFreight.setText(String.format("%s元", NumberUtil.trim0(total)));
                break;
            case CargoPublishEntity.FreightMode.MODE_WEIGHT:
                if (mEntity.getGoodsWeight() == 0) {
                    total = 0;
                    binding.tvReleaseCargoFreight.setText(R.string.default_release_cargo_price);
                } else {
                    total = mEntity.getGoodsWeight() * mEntity.getFreight();
                    binding.tvReleaseCargoFreight.setText(String.format("%s元", NumberUtil.trim0(total)));
                }
                mEntity.setTotalFreight(total);
                break;
            case CargoPublishEntity.FreightMode.MODE_VOLUME:
                if (mEntity.getGoodsVolume() == 0) {
                    total = 0;
                    binding.tvReleaseCargoFreight.setText(R.string.default_release_cargo_price);
                } else {
                    total = mEntity.getGoodsVolume() * mEntity.getFreight();
                    binding.tvReleaseCargoFreight.setText(String.format("%s元", NumberUtil.trim0(total)));
                }
                mEntity.setTotalFreight(total);
                break;
        }
        binding.tvReleaseCargoPrice.setText(String.format("运费 ¥%s/信息费 ¥%s",
                NumberUtil.trim0(mEntity.getTotalFreight()),
                NumberUtil.trim0(mEntity.getMsgPrice())));
    }
}